package com.fly.toolbox;

import com.fly.toolbox.component.AppNotifier;
import com.fly.toolbox.component.FileSelectorComponent;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;

import java.util.Objects;

public class FileSelectorAction extends AnAction {

    private JBPopup popup;

    @Override
    public void actionPerformed(AnActionEvent e) {
        FileSelectorComponent component = new FileSelectorComponent(Objects.requireNonNull(e.getProject()));
        Project project = e.getProject();
        AppNotifier.notifyInfo(project.getName());
        Presentation presentation = e.getPresentation();

        if (popup != null && popup.isVisible()) {
            popup.cancel();
        }
        popup = JBPopupFactory.getInstance()
                .createComponentPopupBuilder(component.getPanel(), component.getPreferredFocusedComponent())
                .setTitle(presentation.getDescription())
                .setResizable(true)
                .setMovable(true)
                .setRequestFocus(true)
                .setCancelOnClickOutside(false)
                .setCancelOnWindowDeactivation(false) // 禁止切换窗口关闭
                .createPopup();
        popup.showCenteredInCurrentWindow(Objects.requireNonNull(project));
    }


}

